using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;
namespace DAL
{
   public class tbMienDAL
    {
        private SqlConnection cn = null;
        public tbMienDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbMien m)
        {
            SqlCommand cm = new SqlCommand("sp_mien_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@tenmien", m.Tenmien);
            cm.ExecuteNonQuery();
        }
        public void Update(tbMien m)
        {
            SqlCommand cm = new SqlCommand("sp_mien_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@mamien", m.Mamien);
            cm.Parameters.AddWithValue("@tenmien", m.Tenmien);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("sp_mien_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@mamien", iD);
            cm.ExecuteNonQuery();
        }
        public tbMienCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_mien_Get_2000", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbMienCollection mColl = new tbMienCollection();

            while (dr.Read()) 
            {
                tbMien m = new tbMien();
                m.Mamien = int.Parse(dr.GetValue(0).ToString());
                m.Tenmien= dr.GetValue(1).ToString();
                mColl.Add(m);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return mColl;
        }
    }
}
